﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using GaiNo.Core.Helpers;
namespace GaiNo.Core.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// Jerome.
        /// 
        /// Get true length of a string.
        /// A Chinese character would be calculated to 2 others would be calculated to 1.
        /// </summary>
        static public int GetTrueLength(this string str)
        {
            // str 字符串
            // return 字符串的字节长度
            int lenTotal = 0;
            int n = str.Length;
            string strWord = "";
            int asc;
            for (int i = 0; i < n; i++)
            {
                strWord = str.Substring(i, 1);
                asc = Convert.ToChar(strWord);
                if (asc < 0 || asc > 127)
                    lenTotal = lenTotal + 2;
                else
                    lenTotal = lenTotal + 1;
            }

            return lenTotal;
        }

        static public string GetMixedString(this string str, int len)
        {
            string result = null;// 最终返回的结果
            int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度
            int charLen = str.Length;// 把字符平等对待时的字符串长度
            int byteCount = 0;// 记录读取进度
            int pos = 0;// 记录截取位置
            if (byteLen > len)
            {
                for (int i = 0; i < charLen; i++)
                {
                    if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2
                        byteCount += 2;
                    else// 按英文字符计算加1
                        byteCount += 1;
                    if (byteCount > len)// 超出时只记下上一个有效位置
                    {
                        pos = i;
                        break;
                    }
                    else if (byteCount == len)// 记下当前位置
                    {
                        pos = i + 1;
                        break;
                    }
                }

                if (pos >= 0)
                    result = str.Substring(0, pos);
            }
            else
                result = str;

            return result;
        }


        static public string GetValueByString(string str, string arg)
        {
            Regex re = new Regex("[&]?" + arg + "=[^&]+", RegexOptions.None);
            string rst = string.Empty;
            if (re.IsMatch(str))
            {
                rst = re.Match(str).Value;
                rst = rst.Substring(rst.IndexOf("=") + 1);
            }
            return rst;
        }

        #region Extensions
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        public static string FormatString(this string str, params object[] strings)
        {
            return string.Format(str, strings);
        }

        public static string Join(this string[] array, string splitter)
        {
            return string.Join(splitter, array);
        }

        public static string Join(this string[] array, char splitter)
        {
            return string.Join(splitter.ToString(), array);
        }


        /// <summary>
        /// 从字符转换为Byte，如果格式不对，返回Null。
        /// </summary>
        /// <param name="str">要转换的字符</param>
        /// <returns></returns>
        public static byte? ToByte(this string str)
        {
            byte rst = byte.MaxValue;
            byte.TryParse(str, out rst);
            if (rst == byte.MinValue)
                return null;
            return rst;
        }

        /// <summary>
        /// 从字符转换为Byte，如果格式不对，返回默认值。
        /// </summary>
        /// <param name="str">要转换的字符</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static byte ToByte(this string str, byte defaultValue)
        {
            byte.TryParse(str, out defaultValue);
            return defaultValue;
        }

        /// <summary>
        /// 从字符转换为整型，如果格式不对，返回Null。
        /// </summary>
        /// <param name="str">要转换的字符</param>
        /// <returns></returns>
        public static int? ToInt(this string str)
        {
            int rst = int.MinValue;
            int.TryParse(str, out rst);
            if (rst == int.MinValue)
                return null;
            return rst;
        }

        /// <summary>
        /// 从字符转换为整型，如果格式不对，返回默认值。
        /// </summary>
        /// <param name="str">要转换的字符</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static int ToInt(this string str, int defaultValue)
        {
            int.TryParse(str, out defaultValue);
            return defaultValue;
        }

        /// <summary>
        /// 从字符转换为Decimal,如果格式不对，返回Null。
        /// </summary>
        /// <param name="str">要转换的字符</param>
        /// <returns></returns>
        public static decimal? ToDecimal(this string str)
        {
            decimal rst = decimal.MinValue;
            decimal.TryParse(str, out rst);
            if (rst == decimal.MinValue)
                return null;
            return rst;
        }

        /// <summary>
        /// 从字符转换为Decimal,如果格式不对，返回默认值。
        /// </summary>
        /// <param name="str">要转换的字符</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static decimal ToDecimal(this string str, decimal defaultValue)
        {
            decimal.TryParse(str, out defaultValue);
            return defaultValue;
        }

        public static DateTime? ToDateTime(this string str)
        {
            DateTime rst = DateTime.MaxValue;
            DateTime.TryParse(str, out rst);
            if (rst == DateTime.MaxValue)
                return null;
            return rst;
        }

        public static DateTime ToDateTime(this string str, DateTime defaultValue)
        {
            DateTime.TryParse(str, out defaultValue);
            return defaultValue;
        }
        public static string Cut(this string str, int length)
        {
            if (str.IsNullOrEmpty())
                return string.Empty;
            int lenTotal = 0;
            int n = str.Length;
            string strWord = string.Empty;
            int asc;
            for (int i = 0; i < n; i++)
            {
                strWord = str.Substring(i, 1);
                asc = Convert.ToChar(strWord);

                if (asc < 0 || asc > 127)
                    lenTotal = lenTotal + 2;
                else
                    lenTotal = lenTotal + 1;

                if (lenTotal > length)
                    return str.Substring(0, i - 3) + "...";
            }
            return str;
        }
        public static bool IsEmail(this string str)
        {
            return RegexHelper.IsEmail(str);
        }
        public static bool IsNum(this string str)
        {
            return RegexHelper.IsNum(str);
        }
        public static bool IsImageFileName(this string str)
        {
            if (str.LastIndexOf('.') == -1)
                return false;

            str = str.Substring(str.LastIndexOf(".") + 1);
            str = str.ToUpper();

            if (str == "GIF" || str == "JPG" || str == "PNG")
                return true;

            return false;
        }
        public static bool IsFlashFileName(this string str)
        {
            if (str.LastIndexOf('.') == -1)
                return false;

            str = str.Substring(str.LastIndexOf(".") + 1);
            str = str.ToUpper();

            if (str == "SWF")
                return true;

            return false;
        }
        public static string AddLeftTrim(this string str, int count)
        {
            return AddLeftTrim(str, count, string.Empty);
        }
        public static string AddLeftTrim(this string str, int count, string chr)
        {
            for (int i = 0; i < count; i++)
            {
                if (chr.IsNullOrEmpty())
                    str = str.Insert(0, "&nbsp;");
                str = str.Insert(0, chr);
            }
            return str;
        }
        public static string[] ToStringArrery(this string str, string separator)
        {
            if (!str.IsNullOrEmpty())
                return str.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
            return null;
        }

        #endregion



    }
}
